package com.kye.express.business.jd.address_select;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.kye.base.Logger;
import com.kye.base.utils.GsonUtils;
import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.databinding.ActivityAddressBinding;
import com.kye.express.utils.ToolbarManager;
import com.kye.model.bean.address.AddressBean;
import com.kye.model.bean.user.User;

import java.util.ArrayList;

import static com.kye.express.business.jd.address_select.AddressSelectLevel.ADDRESS_LEVEL_2_CITY;
import static com.kye.express.business.jd.address_select.AddressSelectLevel.ADDRESS_LEVEL_3_COUNTRY;
import static com.kye.express.business.jd.address_select.AddressSelectLevel.ADDRESS_LEVEL_4_TOWN;

/**
 * 跨越地址选择
 */
public class AddressSelectActivity extends BaseToolbarActivity implements AddressSelectConstract.AddressView, AddressSelectFragment.OnAddressListOptionListener {

    private final static String TAG_SELECT_LEVEL = "TAG_SELECT_LEVEL";
    private AddressSelectConstract.AddressPresenter presenter;
    private ActivityAddressBinding binding;

    // todo : 地址选择级别
    private AddressSelectLevel addressSelectLevel;
    // todo : 当前显示的地址层级
    private AddressSelectLevel addressCurrentLevel;

    private AddressBean selectedProvince;
    private AddressBean selectedCity;
    private AddressBean selectedCountry;
    private AddressBean selectedTown;

    public static void startActivity(Activity activity, int requestCode, AddressSelectLevel addressSelectLevel) {
        Intent intent = new Intent(activity, AddressSelectActivity.class);
        intent.putExtra(TAG_SELECT_LEVEL, addressSelectLevel);
        activity.startActivityForResult(intent, requestCode);
    }

    public static Intent newIntent(Context context, AddressSelectLevel addressSelectLevel) {
        Intent intent = new Intent(context, AddressSelectActivity.class);
        intent.putExtra(TAG_SELECT_LEVEL, addressSelectLevel);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            addressSelectLevel = (AddressSelectLevel) getIntent().getSerializableExtra(TAG_SELECT_LEVEL);
        } else {
            addressSelectLevel = (AddressSelectLevel) savedInstanceState.getSerializable(TAG_SELECT_LEVEL);
        }
        binding = DataBindingUtil.setContentView(this, R.layout.activity_address);
        binding.toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        ToolbarManager.setToolbar(this, binding.toolbar);
        initViews();
        initEvents();
        initData();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putSerializable(TAG_SELECT_LEVEL, addressSelectLevel);
        super.onSaveInstanceState(outState);
    }

    private void initViews() {

    }

    private void initEvents() {

    }

    private void initData() {
        setResult(Activity.RESULT_CANCELED);
        addFragment(AddressSelectLevel.ADDRESS_LEVEL_1_PROVINCE, 0);
    }

    @Override
    public void onUser(User user) {

    }

    public void addFragment(AddressSelectLevel addressSelectLevel, int id) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        AddressSelectFragment addressSelectFragment = AddressSelectFragment.newInstanceForSelection(addressSelectLevel, id);
        addressSelectFragment.setOnAddressListOptionListener(this);
        ft.replace(R.id.container, addressSelectFragment);
        ft.addToBackStack(null);//不需要返回上一层把这行去掉
        ft.commit();
    }

    /**
     * 后退的时候按栈回退
     */
    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStackImmediate();
        } else {
            finish();
        }
    }

    @Override
    public void onItemSelected(AddressSelectLevel currentSelected, AddressBean addressBean, boolean finish) {
        Logger.d("onItemSelected : \n" + GsonUtils.toJsonString(addressBean, true));
        addressCurrentLevel = currentSelected;
        switch (currentSelected) {
            case ADDRESS_LEVEL_1_PROVINCE:
                selectedProvince = addressBean;
                if (addressSelectLevel == addressCurrentLevel) {
                    ArrayList<AddressBean> addressBeans = new ArrayList<>();
                    addressBeans.add(selectedProvince);
                    Intent data = new Intent();
                    data.putParcelableArrayListExtra("data", addressBeans);
                    setResult(Activity.RESULT_OK, data);
                    finish();
                } else {
                    addFragment(ADDRESS_LEVEL_2_CITY, addressBean.getId());
                }
                break;
            case ADDRESS_LEVEL_2_CITY:
                selectedCity = addressBean;
                if (addressSelectLevel == addressCurrentLevel) {
                    ArrayList<AddressBean> addressBeans = new ArrayList<>();
                    addressBeans.add(selectedProvince);
                    addressBeans.add(selectedCity);
                    Intent data = new Intent();
                    data.putParcelableArrayListExtra("data", addressBeans);
                    setResult(Activity.RESULT_OK, data);
                    finish();
                } else {
                    addFragment(ADDRESS_LEVEL_3_COUNTRY, addressBean.getId());
                }
                break;
            case ADDRESS_LEVEL_3_COUNTRY:
                selectedCountry = addressBean;
                if (addressSelectLevel == addressCurrentLevel) {
                    ArrayList<AddressBean> addressBeans = new ArrayList<>();
                    addressBeans.add(selectedProvince);
                    addressBeans.add(selectedCity);
                    addressBeans.add(selectedCountry);
                    Intent data = new Intent();
                    data.putParcelableArrayListExtra("data", addressBeans);
                    setResult(Activity.RESULT_OK, data);
                    finish();
                } else {
                    /**
                     * 选中第三级地址需要判断处理
                     */
                    if (finish) {
                        // 当前的第三级地址没有第四级地址
                        ArrayList<AddressBean> addressBeans = new ArrayList<>();
                        addressBeans.add(selectedProvince);
                        addressBeans.add(selectedCity);
                        addressBeans.add(selectedCountry);
                        Intent data = new Intent();
                        data.putParcelableArrayListExtra("data", addressBeans);
                        setResult(Activity.RESULT_OK, data);
                        finish();
                    } else {
                        addFragment(ADDRESS_LEVEL_4_TOWN, addressBean.getId());
                    }
                }
                break;
            case ADDRESS_LEVEL_4_TOWN:
                selectedTown = addressBean;
                if (addressSelectLevel == addressCurrentLevel) {
                    ArrayList<AddressBean> addressBeans = new ArrayList<>();
                    addressBeans.add(selectedProvince);
                    addressBeans.add(selectedCity);
                    addressBeans.add(selectedCountry);
                    addressBeans.add(selectedTown);
                    Intent data = new Intent();
                    data.putParcelableArrayListExtra("data", addressBeans);
                    setResult(Activity.RESULT_OK, data);
                    finish();
                }
                break;
        }
    }
}
